package com.jason.platform.supervision.activity;

import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import com.hzlj.supervision.changsha.R;
import com.jason.platform.supervision.base.Constant;
import com.jason.platform.supervision.beans.LeaveSignBean;
import com.jason.platform.supervision.beans.LeaveSignResp;
import com.jason.platform.supervision.task.QueryLeaveSignDetailTask;
import com.jason.platform.supervision.task.Task;
import com.jason.platform.supervision.task.TaskListener;
import com.jason.platform.supervision.widght.TitleBar;

/**
 * @ClassName: LeaveSignDetailActivity
 * @Description: 请假签到详情
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2015-7-25 09:47:20
 */
public class LeaveSignDetailActivity extends BaseActivity implements View.OnClickListener,TaskListener {

    private static final String TAG = LeaveSignDetailActivity.class.getSimpleName();

    public static final String KEY_LEAVE_SIGN = "key_leave_sign";

    private TitleBar mTitleBar;

    private MyHandler<LeaveSignDetailActivity> mHandler = new MyHandler<LeaveSignDetailActivity>(this);

    private TextView mNameTextView,mBeginTextView,mEndTextView,mDaysTextView,mReasonTextView,mLeaveTextView,mSickTextView,mLeaveDateTextView,mSickDateTextView;

    private LeaveSignBean mLeaveSignBean;
    private String mLeaveSignId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_leave_sign_detail);
        super.onCreate(savedInstanceState);
        mTitleBar = (TitleBar) findViewById(R.id.title_bar);
        mTitleBar.bindActivity(this);
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        mNameTextView = (TextView) findViewById(R.id.leave_sign_detail_name);
        mBeginTextView = (TextView) findViewById(R.id.leave_sign_detail_begin);
        mEndTextView = (TextView) findViewById(R.id.leave_sign_detail_end);
        mDaysTextView = (TextView) findViewById(R.id.leave_sign_detail_days);
        mReasonTextView = (TextView) findViewById(R.id.leave_sign_detail_reason);
        mLeaveTextView = (TextView) findViewById(R.id.leave_sign_detail_leave);
        mSickTextView = (TextView) findViewById(R.id.leave_sign_detail_sick);
        mLeaveDateTextView = (TextView) findViewById(R.id.leave_sign_detail_leave_date);
        mSickDateTextView = (TextView) findViewById(R.id.leave_sign_detail_sick_date);
    }

    @Override
    protected void initData() {
        super.initData();
        mLeaveSignBean = (LeaveSignBean) getIntent().getSerializableExtra(KEY_LEAVE_SIGN);
        if (mLeaveSignBean != null){
            mLeaveSignId = String.valueOf(mLeaveSignBean.getId());
        }
        updateData();
        QueryLeaveSignDetailTask task = new QueryLeaveSignDetailTask(this);
        task.type = Constant.Manager.GET_LEAVE_DETAIL;
        task.mParams.put("id", mLeaveSignId);
        task.execute();
    }

    private void updateData(){
        if (mLeaveSignBean == null){
            return;
        }
        mNameTextView.setText(mLeaveSignBean.getName());
        mBeginTextView.setText(mLeaveSignBean.getStartTime());
        mEndTextView.setText(mLeaveSignBean.getEndTime());
        mDaysTextView.setText(mLeaveSignBean.getLeaveDays());
        mReasonTextView.setText(mLeaveSignBean.getLeaveReason());
        mLeaveTextView.setText(mLeaveSignBean.getIsLeaveSign());
        mSickTextView.setText(mLeaveSignBean.getIsCancel());
        mLeaveDateTextView.setText(mLeaveSignBean.getLeaveSignTime());
        mSickDateTextView.setText(mLeaveSignBean.getCancelTime());
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.search_header_search_btn){
            //TODO 调用接口，进行搜索
        }
    }

    @Override
    public void onTaskResult(Task task) {
        Message msg = new Message();
        msg.obj = task;
        mHandler.sendMessage(msg);
    }

    @Override
    void handleMessage(BaseActivity activity, Message msg) {
        Task task = (Task) msg.obj;
        if (task.type == Constant.Manager.GET_LEAVE_DETAIL) {
            if (task.isSuccess) {
                LeaveSignResp resp = (LeaveSignResp) task.resData;
                mLeaveSignBean = resp.getValues().get(0);
                updateData();
            }
        }
    }
}
